﻿/*    
Nemulation: A .NET based framework/platform for virtualization applications
Copyright (C) 2011 Bryan Perris

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NEmu;

namespace NEmu.Services
{
    public sealed class VMHostContainer : ICollection<VirtualMachine>
    {
        Dictionary<int, VirtualMachine> m_Machines = new Dictionary<int, VirtualMachine>();

        #region ICollection<VirtualMachine> Members

        public void Add(VirtualMachine item)
        {
            m_Machines.Add(item.GetHashCode(), item);
        }

        public void Clear()
        {
            m_Machines.Clear();
        }

        public bool Contains(VirtualMachine item)
        {
            return m_Machines.ContainsValue(item);
        }

        public void CopyTo(VirtualMachine[] array, int arrayIndex)
        {
            Array.Copy(m_Machines.ToArray(), 0, array, arrayIndex, array.Length);
        }

        public int Count
        {
            get { return m_Machines.Count; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public bool Remove(VirtualMachine item)
        {
            return m_Machines.Remove(item.GetHashCode());
        }

        #endregion

        #region IEnumerable<VirtualMachine> Members

        public IEnumerator<VirtualMachine> GetEnumerator()
        {
            return m_Machines.Values.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return m_Machines.Values.GetEnumerator();
        }

        #endregion

        public void SetState(VirtualMachine machine, RunState state)
        {
            Get(machine.GetHashCode()).SetState(state);
        }

        public void SetState(int hash, RunState state)
        {
            Get(hash).SetState(state);
        }

        public void SetState(RunState state)
        {
            foreach (VirtualMachine machine in m_Machines.Values)
            {
                machine.SetState(state);
            }
        }

        public void StopAllMachines()
        {
            SetState(RunState.Stopped);
        }

        public VirtualMachine Get(int hash)
        {
            VirtualMachine _machine;

            if (m_Machines.TryGetValue(hash, out _machine))
            {
                return _machine;
            }
            else
            {
                throw new KeyNotFoundException("hash");
            }
        }
    }
}
